/*
 * @Author: hanxin 
 * @Date: 2021-08-12 17:23:57 
 * @Last Modified by: hanxin
 * @Last Modified time: 2021-08-15 23:07:47
 * socket线程类
 * 获得每一个客户端的socket做成线程保持通讯
 */
package ServerService;

import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Iterator;

import common.Message;
import common.MessageType;

public class serverConnectClientThread extends Thread {
    private static ArrayList<Message> arrayList = new ArrayList<>();
    Socket socket = null;
    private String uid = null;

    public serverConnectClientThread(Socket socket, String uid) {
        this.socket = socket;
        this.uid = uid;

    }

    @Override
    public void run() {
        while (true) {
            try {
                System.out.println("用户 " + uid + "取得Socket....Connecting... 线程为:" + Thread.currentThread());
                ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
                Message mess = (Message) ois.readObject();

                if (mess.getMessType().equals(MessageType.MESSAGE_GET_ONLINE_LIST)) {
                    System.out.println(uid + " 取得在线用户列表");

                    // 遍历socket线程集合拿到在线用户id
                    String onlinelist = manageClientThreads.retrun_Online_List();

                    // 构建返回的message对象
                    Message messNew = new Message();
                    messNew.setContent(onlinelist);
                    messNew.setMessType(MessageType.MESSAGE_RET_ONLINE_LIST);
                    messNew.setGetter(mess.getSender());// 回复该uid的请求

                    ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
                    oos.writeObject(messNew);

                } else if (mess.getMessType().equals(MessageType.MESSAGE_CLIENT_EXIT)) {

                    System.out.println(mess.getSender() + " 退出系统");
                    manageClientThreads.removeThread(mess.getSender());

                    socket.close();// 关闭要退出系统的客户端的socket
                    break;// 退出线程

                } else if (mess.getMessType().equals(MessageType.MESSAGE_COMM_MES)) {
                    System.out.println(mess.getSender() + " 发送信息到" + mess.getGetter());

                    if (manageClientThreads.returnthread(mess.getGetter()) != null) {// 判断要发送的用户是否在线

                        // 将请求message对象转发到对应客户端的socket线程
                        ObjectOutputStream oos = new ObjectOutputStream(
                                manageClientThreads.returnthread(mess.getGetter()).socket.getOutputStream());
                        // 转发message对象信息到指定的客户端socket
                        oos.writeObject(mess);

                    } else {
                        Message messerror = new Message();
                        messerror.setContent("user_not_online_or_user_doesNot_exist.Save_in_offline_message");

                        messerror.setMessType(MessageType.MESSAGE_COMM_MES);

                        ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
                        oos.writeObject(messerror);

                        // 转存进入离线消息集合
                        arrayList.add(mess);
                        offline_Message_Save.saveMessage(mess.getGetter(), arrayList);
                    }

                } else if (mess.getMessType().equals(MessageType.MESSAGE_COMM_MES_TOALL)) {
                    // 遍历管理socket线程的hash表,拿到所有用户的id(k值)
                    Hashtable<String, serverConnectClientThread> hashtable = manageClientThreads.hashtable;
                    Iterator<String> iterator = hashtable.keySet().iterator();
                    while (iterator.hasNext()) {
                        String onlineUser = iterator.next().toString();

                        if (!onlineUser.equals(mess.getSender())) {// 不转发消息给发送者本身
                            // 拿到所有用户(客户端)的socket,遍历发送消息
                            ObjectOutputStream oos = new ObjectOutputStream(
                                    manageClientThreads.returnthread(onlineUser).socket.getOutputStream());
                            oos.writeObject(mess);
                        }

                    }
                } else if (mess.getMessType().equals(MessageType.MESSAGE_FILE_MES)) {

                    if (manageClientThreads.returnthread(mess.getGetter()) != null) {

                        System.out.println(mess.getSender() + " 向 " + mess.getGetter() + " 传文件");
                        ObjectOutputStream oos = new ObjectOutputStream(
                                manageClientThreads.returnthread(mess.getGetter()).socket.getOutputStream());

                        oos.writeObject(mess);
                    } else {
                        Message messerror = new Message();
                        messerror.setContent("user_not_online_or_user_doesNot_exist.Save_in_offline_message");

                        messerror.setMessType(MessageType.MESSAGE_COMM_MES);

                        ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
                        oos.writeObject(messerror);

                        // 转存进入离线消息集合
                        arrayList.add(mess);
                        offline_Message_Save.saveMessage(mess.getGetter(), arrayList);
                    }

                }

            } catch (Exception e) {

                e.printStackTrace();
                System.out.println("用户连接异常");

                manageClientThreads.removeThread(uid);
                try {
                    socket.close();
                } catch (Exception er) {
                    er.printStackTrace();
                }
                break;// 如果用户异常关闭客户端(没有发送CLIENT_Exit消息),直接结束线程
            }
        }

    }
}
